<?php

header("Content-Type: application/javascript");

$files = scandir(ROOT . DS . 'app' . DS . 'action' . DS . $controller);

echo "var " . $controller . " = {\n";
$len = count($files);
$i = 0;
for (; $i < $len; $i++) {
    $file = $files[$i];
    $pos = strpos($file, ".php");
    //echo " \nfile: " . $file . " " . strlen($file) . " " . $pos . " ";
    if ($pos) {
        require_once(ROOT . DS . 'app' . DS . 'action' . DS . $controller . DS . $file);
        $file = substr($file, 0, strlen($file) - 4);

        echo $file . ": function(";
        $actionName = ucfirst($file);
        $reflector = new ReflectionClass($actionName);
        $parameters = $reflector->getMethod('setParams')->getParameters();
        $action = new $actionName();
        $method = $action->getHTTPMethod();
        
        foreach ($parameters as $param) {
            if($param->getName() != 'param'){
                echo " ".$param->getName() . ",";
            }
        }

        if ($method == POST) {
            echo " param, handler){\n"; 
        } else {
            echo " handler){\n";
        }
        echo "\t$." . $method . "('app/" . $controller . "/" . $file . "'";
        foreach ($parameters as $param) {
            if($param->getName() != 'param'){
                echo " + '/' + " . $param->getName();
            }
        }
        if ($method == POST) {
            echo ", param, handler);\n";
        } else {
            echo ", handler);\n";
        }
        if ($len - 1 == $i) {
            echo "}\n";
        } else {
            echo "},\n";
        }
    }
}
echo "};";